package com.zhr.liquwang.view.frg;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONObject;
import com.zhr.liquwang.R;
import com.zhr.liquwang.api.UrlManager;
import com.zhr.liquwang.entity.Goods;
import com.zhr.liquwang.entity.HomeBanner;
import com.zhr.liquwang.entity.XianShi;
import com.zhr.liquwang.view.activity.GoodsActivity;
import com.zhr.liquwang.view.adapter.HomeAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {

    private List<HomeBanner> banners;
    private List<XianShi> xianShis;
    private List<Goods.ResultBean> data;
    private HomeAdapter adapter;
    @BindView(R.id.rv_home)
    RecyclerView rvHome;

    public HomeFragment() {
        // Required empty public constructor
    }

    public static HomeFragment newInstance() {

        Bundle args = new Bundle();

        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initView(View view) {
        ButterKnife.bind(this, view);
        banners = new ArrayList<>();
        xianShis = new ArrayList<>();
        data = new ArrayList<>();
        adapter = new HomeAdapter(getContext(), banners, xianShis, data, new HomeAdapter.HomeGoodsClick() {

            @Override
            public void click(int shopId) {
                Intent intent = new Intent(getActivity(), GoodsActivity.class);
                intent.putExtra("shopid", shopId);
                startActivity(intent);
            }
        });
        rvHome.setLayoutManager(new LinearLayoutManager(getContext()));
        rvHome.setAdapter(adapter);
    }


    private void initData() {
        RequestParams params = new RequestParams();
        params.addFormDataPart("pfid", 1);
        HttpRequest.post(UrlManager.BASE_BANNER_URL, params, new BaseHttpRequestCallback<String>() {
            @Override
            protected void onSuccess(String s) {
                super.onSuccess(s);
                List<HomeBanner> homeBanners = JSONObject.parseArray(s, HomeBanner.class);
                banners.addAll(homeBanners);
                adapter.notifyDataSetChanged();
            }
        });
        HttpRequest.get(UrlManager.XIANSHI_URL, new BaseHttpRequestCallback<String>() {
            @Override
            protected void onSuccess(String s) {
                super.onSuccess(s);
                List<XianShi> shis = JSONObject.parseArray(s, XianShi.class);
                xianShis.addAll(shis);
                adapter.notifyDataSetChanged();
            }
        });
        RequestParams params3 = new RequestParams();
        params3.addFormDataPart("pageindex", 1);
        params3.addFormDataPart("pagesize", 3);
        HttpRequest.post(UrlManager.HOME_GOODS_URL, params3, new BaseHttpRequestCallback<Goods>() {
            @Override
            protected void onSuccess(Goods goods) {
                super.onSuccess(goods);
                data.addAll(goods.getResult());
                adapter.notifyDataSetChanged();
            }
        });

    }

}
